/*
* ChimneyController.java
* Created on  202015/6/15 16:35
* 版本       修改时间          作者      修改内容
* V1.0.1    202015/6/15       longshihui    初始版本
*
*/

package com.infitecs.eyas.combustion;

import com.infitecs.eyas.combustion.model.CalculateBurn;
import com.infitecs.eyas.combustion.model.CalculateChimney;
import com.infitecs.eyas.combustion.model.CalculateClean;
import com.infitecs.eyas.combustion.service.ChimneyService;
import com.infitecs.eyas.combustion.service.CleanService;
import com.infitecs.eyas.expression.service.ExpressionService;
import com.infitecs.eyas.power.dao.OperateFieldDao;
import com.infitecs.eyas.power.dao.PowerDao;
import com.infitecs.eyas.power.model.OperateField;
import com.infitecs.eyas.power.model.Power;
import com.infitecs.eyas.session.SessionService;
import com.infitecs.eyas.session.model.Param;
import com.infitecs.eyas.step.service.StepService;
import com.infitecs.krill.ajax.response.AjaxResponse;
import com.infitecs.krill.ajax.response.ReturnState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 类的描述信息
 *
 * @author longshihui
 * @version 1.0.1
 */
@Controller
@RequestMapping("/combustion/chimney")
public class ChimneyController {
    @Autowired
    private ChimneyService chimneyService;

    @Autowired
    private SessionService sessionService;

    @Autowired
    private ExpressionService expressionService;
    @Autowired
    private StepService stepService;
    @Autowired
    private OperateFieldDao operateFieldDao;
    @Autowired
    private PowerDao powerDao;
    @Autowired
    private CleanService cleanService;

    @RequestMapping("")
    public String getChimneyPage(HttpServletRequest request) {
        String cId = (String) request.getSession().getAttribute("caseId");
        if (cId == null) {
            return "combustion/chimney";
        }
        int caseId = Integer.parseInt(cId);
        int step = stepService.getMzFxStep(caseId);
        //判断当前方案流程到哪里
        if (step < 6) {
            request.setAttribute("step", step);
            return "combustion/chimney";
        }
        step = stepService.geRsStep(caseId);
        if (step < 11) {
            request.setAttribute("step", step);
            return "combustion/chimney";
        }
        Power powerTitle = powerDao.getCaseById(cId);
        OperateField operateField = operateFieldDao.getOperateField(caseId);
        CalculateChimney calculateChimney = chimneyService.getCalculateChimney();
        if(calculateChimney==null){
            Map<String, Double> dataMap = cleanService.getCleanDataByCaseId(caseId);
            request.setAttribute("dataMap", dataMap);
        }else {
            Map<String, Double> dataMap = chimneyService.getChimneyByCaseId(caseId);
            request.setAttribute("dataMap", dataMap);
        }
        request.setAttribute("powerTitle", powerTitle);
        request.setAttribute("calculateChimney", calculateChimney);
        request.setAttribute("caseId", caseId);
        request.setAttribute("operateField", operateField);
        request.setAttribute("message", request.getParameter("message"));
        return "combustion/chimney";
    }

    @RequestMapping(value = "/saveValueOfChimneyEx", method = {RequestMethod.POST})
    @ResponseBody
    public void saveValueOfChimneyEx(CalculateChimney calculateChimney) {
        chimneyService.saveValueOfChimney(calculateChimney);
    }

    @RequestMapping(value = "/getCalculateInit", method = {RequestMethod.POST})
    @ResponseBody
    public void getCalculateInit() {
        sessionService.putChimneyValue(sessionService.getCaseId());
    }

    @RequestMapping(value = "/getAutoValue", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResponse getAutoValue() {
        AjaxResponse ajaxResponse = new AjaxResponse();
        CalculateChimney calculateChimney = new CalculateChimney();
        Double e327 = expressionService.calculate(new Param("RE327"));
        ajaxResponse.addAttribute("e327",e327);
        calculateChimney.setParamName("E327");
        calculateChimney.setParamValue(e327);
        chimneyService.saveValueOfChimney(calculateChimney);
        Double e328 = expressionService.calculate(new Param("RE328"));
        ajaxResponse.addAttribute("e328",e328);
        calculateChimney.setParamName("E328");
        calculateChimney.setParamValue(e328);
        chimneyService.saveValueOfChimney(calculateChimney);
        return ajaxResponse;
    }

    @RequestMapping(value = "chimneySave", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResponse chimneySave(CalculateChimney calculateChimney) {
        try {
            chimneyService.chimneySave(calculateChimney);
            return new AjaxResponse();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResponse(ReturnState.ERROR, "请检查填写数据是否有误，操作失败");
        }
    }

    @RequestMapping(value = "chimneyValidate", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResponse chimneyValidate(CalculateChimney calculateChimney) {
        try {
            return chimneyService.chimneyValidate(calculateChimney);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResponse(ReturnState.ERROR, "请检查填写数据是否有误，验证失败");
        }
    }
}
